<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <cost-plan v-model="model.productCodes" v-bind="modalProps" rowKey="code" />
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction, postFormAction, putAction, postAction } from '@/api/manage'
  import costPlan from '@/views/logistics/sales/modules/costPlan'

  export default {
    name: 'SysMerchantOrgFormChild',
    components: {
      costPlan,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          assignProduct: "/logistics/product/logisticsProduct/assignProduct",
        },
        modalProps: {
          modalPropsCustom: true,
          columns: [
            {
              align: "center",
              dataIndex: "name",
              title: "物流产品名称",
              width: "33%",
              isShow: true
            },
            {
              align: "center",
              dataIndex: "channelName",
              title: "渠道名称",
              width: "33%",
              isShow: true
            },
            {
              align: "center",
              dataIndex: "costPriceId_dictText",
              title: "成本价方案",
              width: "33%",
              isShow: true
            }
          ],
          valueKey: "codes",
          listUrl: "/logistics/product/logisticsProduct/list",
          multiple: true,
          queryParamConfig: {
            ableStatus: '1,2,3'
          },
          queryConfig: [
            {key: 'name', label: '物流产品'},
            {key: 'channelId', label: '渠道名称', dictCode: "hz_logistics_channel,channel_name,id,logistics,is_enable='Y'"},
            {key: 'costPriceId', label: '成本价方案', dictCode: "hz_logistics_cost_price,name,id,logistics,able_status='2'"},
          ]
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record, ids) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(this.model.id){
          this.model.productCodes = ids;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            let httpurl = '';
            let Action;
            let params = Object.assign({}, this.model);
            if(!this.model.id){
              httpurl+=this.url.add;
              Action = postAction;
            }else{
              httpurl+=this.url.assignProduct;
              Action = postFormAction;
              params = {
                productCodes: this.model.productCodes,
                ids: this.model.id
              }
            }

            if(!this.model.productCodes){
              this.$message.warning('请选择物流产品');
              return;
            }

            that.confirmLoading = true;
            Action(httpurl, params).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      }
    }
  }
</script>